package gov.pbc.nn.kjgl.item.template.handler;

import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.model.Message;
import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.process.handler.NodeEventHandler;
import com.bstek.uflo.process.node.Node;

import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.kjgl.item.template.dao.ItemApprovalDao;
import gov.pbc.nn.kjgl.item.template.entity.ItemApproval;
import gov.pbc.nn.uflo.entity.Opinion;

@Component(NotifyPromoterActionHandler.BENA_ID)
public class NotifyPromoterActionHandler implements NodeEventHandler {

	public static final String BENA_ID = "gov.pbc.nn.kjgl.item.template.handler.NotifyPromoterActionHandler";

	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreHibernateDao;

	@Autowired
	private ItemApprovalDao itemApprovalDao;

	private static final String SENDER = "系统";

	@Override
	public void enter(Node arg0, ProcessInstance pi, Context context) {
		String businessId = pi.getBusinessId();
		ItemApproval ia = this.itemApprovalDao.get(businessId, ItemApproval.class);
		if(ia.getState()!=20){
			ia.setState(30);
			this.itemApprovalDao.update(ia);
		}
//		Opinion opinion = (Opinion) context.getProcessService().getProcessVariable("opinion", pi);
//		if (opinion != null) {
//			// 审核未通过，则通知申请人（发站内信）
//			String username = pi.getPromoter();
//			String businessId = pi.getBusinessId();
//
//			final Message msg = new Message();
//			msg.setId(UUID.randomUUID().toString());
//			msg.setSendDate(new Date());
//			msg.setSender("系统提示");
//			msg.setReply(false);
//			msg.setRead(false);
//			msg.setTitle(opinion.getProcessName() + "--事项审批被否决！");
//			msg.setContent(opinion.getOpinion());
//			msg.setReceiver(username);
//			coreHibernateDao.doInHibernateSession(new ISessionCallback<Void>() {
//
//				@Override
//				public Void doInSession(Session session) {
//					session.save(msg);
//					return null;
//				}
//
//			});
//		} else {
//			ItemApproval ia = this.itemApprovalDao.get(pi.getBusinessId(), ItemApproval.class);
//			String title = ia.getName() + "流程已结束！";
//			String content = (String) context.getProcessService().getProcessVariable("processResult", pi);
//			final Message msg = new Message();
//			msg.setId(UUID.randomUUID().toString());
//			msg.setSendDate(new Date());
//			msg.setSender(SENDER);
//			msg.setReply(false);
//			msg.setRead(false);
//			msg.setTitle(title);
//			msg.setContent(content);
//			msg.setReceiver(pi.getPromoter());
//			this.coreHibernateDao.doInHibernateSession(new ISessionCallback<Void>() {
//
//				@Override
//				public Void doInSession(Session session) {
//					session.save(msg);
//					return null;
//				}
//
//			});
//		}
	}

	@Override
	public void leave(Node arg0, ProcessInstance pi, Context context) {
		// TODO Auto-generated method stub

	}
}
